<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\index\model\Page;

class Product extends Controller
{
	public $callback;
    //返回商品详情
    public function infor(Request $request)
    {
    	// echo "string";die;
    	$id = $request->param('goods');
    	$this->callback=$request->param('callback');
    	$data = Db::table('product')->where('productId',$id)->find();
    	$arr = $this->chu($data);
    	// print_r($arr);die;
    	$arr['endTime'] = date('Y-m-d',$arr['endTime']);
    	echo $this->callback.'('.json_encode($arr).')';
    }
    public function getOne($id)
    {
    	$data = Db::table('product')->where('productId',$id)->find();
    	$this->chu($data);
    	return $data;
    }
    //商品列表
    public function productList(Request $request)
    {
    	$arr = $request->param();
    	$this->callback=$request->param('callback');
    	array_shift($arr);
    	// print_r($arr);die;
    	$aa=$this->gete($arr);
    	// print_r($aa);die;
    	$model = new Page;
    	// var_dump($aa);die
    	$data=$model->index($aa);
    	echo $this->callback.'('.json_encode($data).')';
    }
    public function chu($arr)
    {
    	// print_r($arr);die;
    		if($arr['startTime']>time()){
    			$arr['bi']='a';//即将上线
    			return $arr;
    		}
    		if($arr['endTime']<time()){
    			$time = $arr['updateTime']+($arr['deadLine']+1)*30;
    			if($time>time()){
    				$arr['bi'] = 'b';//回款中
    			}else{
    				$arr['bi'] = 'c';//已结束
    			}
    			return $arr;
    		}
    		if($arr['surplusMoney']==0){
    			$time = $arr['updateTime']+($arr['deadLine']+1)*30;
    			if($time>time()){
    				$arr['bi'] = 'b';//回款中
    			}else{
    				$arr['bi'] = 'c';//已结束
    			}
    			return $arr;
    		}
    		// print_r($arr);die;
    		if($arr['surplusMoney']>0){
    			$arr['bi']=floor((1-$arr['surplusMoney']/$arr['totalSum'])*100);//百分比
    			return $arr;
    		}
    	return '';
    }
    public function gete($arr)
    {

    	if(empty($arr)){
    		return '参数错误';
    	}
    	
    	$data['table']=$arr['table'];
    	// echo "string";die;
    	$data['page']=$arr['page'];
    	$data['size']=$arr['size'];
    	// print_r($data);die;
    	if(isset($arr['name'])){
    		// echo "string";die;
    		// print_r($arr);die;
    		if($arr['name']=='one1'){
    			$data['where']='and deadLine='.$arr['type'].' and status=1';
    			// echo "string";die;
    			return $data;
    		}else if($arr['name']=='one2'){
    			if($arr['type']=='0'){//回款完毕
    				$data['where'] = 'and surplusMoney=0 and ((deadLine+1)*30+updateTime)<'.time().' and status=1';
    				return $data;
    			}else if($arr['type']=='1'){//即将上线
    				$data['where'] = 'and startTime>'.time().' and status=1';
    				return $data;
    			}else if($arr['type']=='2'){//正在募集
    				$data['where'] = ' and startTime<'.time().' and surplusMoney>0 and status=1 and endTime>'.time();
    				return $data;
    			}else if($arr['type']=='3'){//正在回款
    				$data['where'] = 'and surplusMoney=0 and ((deadLine+1)*30+updateTime)>'.time().' and status=1';
    				return $data;
    			}
    		}else if($arr['name']=='one3'){
    				// $data['table'] = $data['table'].' as b left join `order` as o on b.productId=o.pid';
	    			// $data['value'] = 'b.*,sum(o.price) as money';
	    			$data['where']='and retunRate='.$arr['type'].' and status=1';
	    			return $data;
    		}else if($arr['name']=='one4'){
                $data['where'] = 'and status=1';
                return $data;
            }
    	}
    }
}
